package pl.com.hme.mediaplayer.activities;

import java.io.File;
import java.util.Iterator;

import pl.com.hme.mediaplayer.R;
import pl.com.hme.mediaplayer.player.GenericMediaPlayer;
import pl.com.hme.mediaplayer.player.GenericOnCompletionListener;
import pl.com.hme.mediaplayer.player.GenericOnPreparedListener;
import pl.com.hme.mediaplayer.playlist.PlaylistIteratorUtil;
import android.app.Activity;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnVideoSizeChangedListener;
import android.os.Bundle;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.widget.Toast;

public class PlayerActivity extends Activity implements GenericOnCompletionListener, GenericOnPreparedListener, SurfaceHolder.Callback, OnVideoSizeChangedListener {

    private static final String TAG = "PlaybackActivity";

    private String mPlaylistDirectory = "sdcard/video";
    private Iterator<String> mPlayListIterator;

    private SurfaceView mSurfaceView;
    private GenericMediaPlayer mGenericPlayer;

    private String mVideoLink;
    private boolean mPlayerInitialisation = false;
    private boolean mPlayOnStart = true;

    private SurfaceHolder mSurfaceHolder;

    @Override
    protected void onCreate(Bundle savedInstanceState) throws IllegalArgumentException {
        super.onCreate(savedInstanceState);

        Log.d(TAG, "onCreate");

        playerInit();

        setContentView(R.layout.video);

        nextVideo(true);
        
        mSurfaceView = (SurfaceView) findViewById(R.id.surfaceVideoView);
        mSurfaceHolder = mSurfaceView.getHolder();
        mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
        mSurfaceHolder.addCallback(this);

    }

    private void nextVideo(boolean firstRun) {
        if (mPlayListIterator == null || !mPlayListIterator.hasNext()) {
            try{
            mPlayListIterator = PlaylistIteratorUtil.getPlaylistIterator(new File(mPlaylistDirectory));
            }catch(NullPointerException e){
                Toast.makeText(this, getString(R.string.no_dir_error) + " " + mPlaylistDirectory, Toast.LENGTH_LONG).show();
                finish();
            }
        }

        if (mPlayListIterator != null  && mPlayListIterator.hasNext()) {
            mVideoLink = mPlaylistDirectory + "/" + mPlayListIterator.next();
            
            
            if (!firstRun) {
                playerStart();
            }
        }else{
            Toast.makeText(this, getString(R.string.player_error_start), Toast.LENGTH_LONG).show();
        }
    }

    @Override
    protected void onStop() {
        Log.d(TAG, "onStop");

        if (mGenericPlayer.isPlaying() || mPlayerInitialisation == true) {
            mPlayOnStart = true;
        } else {
            mPlayOnStart = false;
        }
        playerRelease();

        super.onStop();
    }

    @Override
    public void onCompletion(GenericMediaPlayer genericPlayer) {
        Log.d(TAG, "onCompletion");

        mPlayerInitialisation = false;

        nextVideo(false);
    }

    @Override
    public void onPrepared(GenericMediaPlayer genericPlayer) {
        Log.d(TAG, "onPrepared");

        mPlayerInitialisation = false;

    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        Log.d(TAG, "Surface Created");

        if (mPlayOnStart == true) {
            mPlayOnStart = false;
            playerStart();
        } else {
            Log.d(TAG, "Player is waiting for Play");
        }
    }

    /**
     * Initializes player and sets it's listeners.
     */
    private void playerInit() {
        Log.d(TAG, "playerInit");
        mGenericPlayer = new GenericMediaPlayer();
        mGenericPlayer.setOnCompletionListener(this);
        mGenericPlayer.setOnPreparedListener(this);
        mGenericPlayer.setOnVideoSizeChangedListener(this);
    }

    private void playerStart() {
        Log.v(TAG, "playerStart");

        try {
            mGenericPlayer.startPlayer(mSurfaceHolder, mVideoLink, 0);

            mPlayerInitialisation = true;
        } catch (Exception e) {
            Toast.makeText(this, getString(R.string.player_error_start), Toast.LENGTH_LONG).show();
        }
    }

    private void playerRelease() {
        mGenericPlayer.releasePlayer();
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        Log.d(TAG, "Surface Destroyed");
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
        Log.d(TAG, "Surface Changed");

    }

    @Override
    public void onVideoSizeChanged(MediaPlayer mp, int width, int height) {
    }

}
